package com.shinektv.vod.ksclib.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FileUtils {
    public static ArrayList<String> ReadTxtFile(String strFilePath) {
        String path = strFilePath;
        ArrayList<String> newList = new ArrayList<String>();
        //打开文件
        File file = new File(path);
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory()) {
            Log.d("TestFile", "The File doesn't not exist.");
        } else {
            InputStream instream = null;
            InputStreamReader inputreader = null;
            BufferedReader buffreader = null;
            try {
                instream = new FileInputStream(file);
                if (instream != null) {
                    inputreader = new InputStreamReader(instream, "gbk");
                    buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while ((line = buffreader.readLine()) != null) {
                        newList.add(line);
                    }
                }
            } catch (FileNotFoundException e) {
                Log.d("TestFile", "The File doesn't not exist. " + e.getMessage());
            } catch (IOException e) {
                Log.d("TestFile", e.getMessage());
            }
            if (buffreader != null) {
                try {
                    buffreader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputreader != null) {
                try {
                    inputreader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (instream != null) {
                try {
                    instream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return newList;
    }
}
